import {$axios} from '@/utils/axios.js';
import config from "@/config";

// 3. 全球确定性预报检验模块

// 3.1 页面加载
export function pageLoadInfo() {
    return $axios.post('/global_deterministic/page_load_info');
}

// 3.2 绘图请求（格点、站点，数据）
export function drawingRequest(type, data) {
    switch (type) {
        case 'grid_analyze':
            return $axios.post('/global_deterministic/grid_analyze', data);
        case 'station_analyze':
            return $axios.post('/global_deterministic/station_analyze', data);
        default:
            return null
    }
}

export function transScdCard(data, options, levelLength, timeLength) {
    const keys = ['data_h', 'data_t', 'data_u', 'data_v', 'data_w'];
    const result = [];
    const number = data[keys[0]].length; // 评分卡的数量
    for (let cardIndex = 0; cardIndex < number; ++cardIndex) {
        const card = [];
        for (let key of keys) {
            let arr = data[key];
            let row = {};

            for (let o = 0; o < options.length; ++o) {
                let val = new Array(levelLength).fill(0).map(a => new Array(timeLength));
                let option = options[o];

                for (let i = 0; i < levelLength; ++i) {
                    for (let j = 0; j < timeLength; ++j) {
                        val[i][j] = arr[cardIndex][o * levelLength * timeLength + i * timeLength + j];
                        val[i][j] = val[i][j] === config.NAN_NUMBER ? null : val[i][j];
                    }
                }

                row[option] = val;
            }
            card.push(row);
        }
        result.push(card);
    }
    return result;
}

// 3.4 综合评分卡
export function scdCard(data) {
    return $axios.post('/global_deterministic/scd_card', data).then(res => {
        // 进行预处理
        const data = res.data;
        const options = ['ACC', 'RMSE', 'BIAS'];
        const levelLength = 12; // level
        const timeLength = 9; // time series
        res.data = transScdCard(data, options, levelLength, timeLength);
        return Promise.resolve(res);
    });
}

// 转换二级图表数据
export function transScdCardSub(data) {
    const metricStr = 'data-metric';
    const diffStr = 'data-diff';
    const keys1 = Object.keys(data[metricStr]);
    // 处理 -9999 >> null
    for (let key of keys1) {
        if (key === 'x') continue;
        data[metricStr][key] = data[metricStr][key].map(i => i === config.NAN_NUMBER ? null : i);
    }

    for (let i = 0; i < data[diffStr].diff.length; ++i) {
        if (data[diffStr].diff[i] === config.NAN_NUMBER) {
            data[diffStr].diff[i] = data[diffStr].upper[i] = data[diffStr].lower[i] = null;
        }
    }
    data.metric = data[metricStr];
    data.diff = data[diffStr];
    delete data[metricStr];
    delete data[diffStr];
}

// 3.4 综合评分卡: 二级图表
export function scdCardSub(data) {
    return $axios.post('/global_deterministic/scd_card_sub', data).then(res => {
        transScdCardSub(res.data)
        return Promise.resolve(res);
    });
}

// 3.4 综合评分卡: 申请置信度数据
export function scdCardGetConfidence() {
    return $axios.post('/global_deterministic/scd_card/get_confidence');
}